package com.goldwind.login.service;

import com.goldwind.login.dao.LoginMapper;
import com.goldwind.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @author 35201
 */
@Service
public class EmailLoginService {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送邮件
     * @param from 发送方
     * @param to 接收方
     * @param subject 主题
     * @param contents 内容
     * @return 返回执行结果状态
     */

    public void sendEmail(String from, String to, String subject, String contents){
        // 创建一个简单消息对象，用于发送简单消息（不带附件或连接等）
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 封装邮件信息
        // 设置发送方
        simpleMailMessage.setFrom(from);
        // 设置接收方
        simpleMailMessage.setTo(to);
        // 设置主题
        simpleMailMessage.setSubject(subject);
        // 设置发送内容
        simpleMailMessage.setText(contents);
        // 发送动作
        javaMailSender.send(simpleMailMessage);
    }

    public boolean isEmailExist(String email)throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
        try{
            User user = loginMapper.queryByEmail(email);
            if(user != null){
                return true;
            }
        }catch (NullPointerException e){
            return false;
        }finally {
            sqlSession.close();
        }
        return false;
    }

    public int insertUser(User user) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
        user.setStatus("0");
        user.setAvatar("https://avrg.oss-cn-hangzhou.aliyuncs.com/avatars/te.jpg");
        //user.setAvatar("https://avrg.oss-cn-hangzhou.aliyuncs.com/avatars/te.jpg");
        //System.out.println(user.toString());
        int flag = loginMapper.insertUser(user);

        sqlSession.commit();
        sqlSession.close();
        return flag;
    }
//    @Test
//    public void insertUser() throws IOException {
//        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
//        User user = new User();
//        user.setUserName("admin");
//        user.setPassword("123456");
//        user.setStatus("0");
//        //System.out.println(loginMapper.queryByEmail("john.doe@example.com").toString());
//        loginMapper.insertUser(user);
//        sqlSession.commit();
//        sqlSession.close();
//    }
}
